package com.book.controller;

import com.book.pojo.Favorites;
import com.book.pojo.FavoritesVo;
import com.book.pojo.Order;
import com.book.pojo.User;
import com.book.service.FavoritesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author icatw
 * @date 2022/4/8
 * @email 762188827@qq.com
 * @apiNote
 */
@Controller
@RequestMapping("/favorites")
@Slf4j
public class FavoritesController {
    @Autowired
    FavoritesService favoritesService;

    /**
     * 收藏夹列表
     *
     * @param session 会话
     * @param model   模型
     * @return {@link String}
     */
    @RequestMapping("/list")
    public String favoritesList(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        //TODO 根据用户id去查关联表信息
        List<FavoritesVo> favoritesVos = favoritesService.findUserFavorites(user.getId());
        model.addAttribute("favorites", favoritesVos);
        return "collect_list";
    }

    /**
     * 收藏
     *
     * @param bookId  书id
     * @param session 会话
     * @return {@link String}
     */
    @PostMapping("/add")
    @ResponseBody
    public String add(Integer bookId, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        favoritesService.updateStatus(userId, bookId);
        return "success";
    }

    /**
     * 取消收藏
     *
     * @param bookId  书id
     * @param session 会话
     * @return {@link String}
     */
    @PostMapping("/delete")
    @ResponseBody
    public String delete(Integer bookId, HttpSession session,Model model) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        favoritesService.updateStatusByUserIdAndBookId(userId, bookId);
        return "success";
    }
}
